/**
 * 
 */
package org.company.management.entity;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Paner
 *
 */
public class SchedulerHour
{
	private String _hourRange;
	private String _formattedHour;
	private boolean _needsExtension;
	private boolean _isValid;
	private HashMap<String, String> _formattedHourParts;
	
	public SchedulerHour (String hourRange )
	{
		_hourRange = hourRange;
		_formattedHour = "";
		
		_formattedHourParts = new HashMap<String, String>();
		_formattedHourParts.put("fromHour", "");
		_formattedHourParts.put("fromMinute", "");
		_formattedHourParts.put("toHour", "");
		_formattedHourParts.put("toMinute", "");
	}
	
	public String getFormattedHour( )
	{
		if(isValid())
		{
			_formattedHour = formatHour();
			needsExtension();
		}
		
		return _formattedHour;
	}
	
	public String getFromHour( )
	{
		return _formattedHourParts.get("fromHour");
	}
	
	public String getFromMinute( )
	{
		return _formattedHourParts.get("fromMinute");
	}
	
	public String getToHour( )
	{
		return _formattedHourParts.get("toHour");
	}
	
	public String getToMinute( )
	{
		return _formattedHourParts.get("toMinute");
	}
	
	public boolean isValid()
	{
		String pattern = "(?<from1>[01]?[0-9]|2[0-4])(:?)(?<from2>[0-5][0-9])?-(?<to1>[01]?[0-9]|2[0-4])(:?)(?<to2>[0-5][0-9])?";
		
		_isValid = _hourRange.matches(pattern);
			
		return _isValid;
		
	}
	

	public boolean getNeedsExtension()
	{
		return _needsExtension;
	}
	
	private String formatHour()
	{
		if(_formattedHour == null || _formattedHour.isEmpty())
		{
			String pattern = "(?<from1>[01]?[0-9]|2[0-4])(:?)(?<from2>[0-5][0-9])?-(?<to1>[01]?[0-9]|2[0-4])(:?)(?<to2>[0-5][0-9])?";
			Pattern hourPattern = Pattern.compile(pattern);
			Matcher m = hourPattern.matcher(_hourRange);
			if (m.matches())
			{
				try
				{
					_formattedHour += m.group("from1");
					_formattedHourParts.put("fromHour", m.group("from1"));
				}
				catch(Exception ex)
				{}
	
				if(!(m.group("from2") == null))
				{
					_formattedHour +=  ":" +m.group("from2") +"-";
					_formattedHourParts.put("fromMinute", m.group("from2"));
				}
				else
				{
					_formattedHour += ":00-";
					_formattedHourParts.put("fromMinute", "00");
				}
				try
				{
					_formattedHour += m.group("to1");
					_formattedHourParts.put("toHour", m.group("to1"));
				}
				catch(Exception ex)
				{}
				if(!(m.group("to2") == null))
				{
					_formattedHour += ":" + m.group("to2");
					_formattedHourParts.put("toMinute", m.group("to2"));
				}
				else
				{
					_formattedHour += ":00";
					_formattedHourParts.put("toMinute", "00");
				}
				
			}
		}
		return _formattedHour;
	}
	
	private boolean needsExtension()
	{
		_needsExtension = false;
		
		int fromHour = 0;
		int fromMinute = 0;
		int toHour = 0;
		int toMinutes = 0;

		try
		{
			fromHour  = Integer.parseInt(_formattedHourParts.get("fromHour"));
			
		}
		catch(Exception ex)
		{}
		try
		{
			fromMinute  = Integer.parseInt(_formattedHourParts.get("fromMinute"));
		}
		catch(Exception ex)
		{

		}
		try
		{
			toHour  = Integer.parseInt(_formattedHourParts.get("toHour"));
		}
		catch(Exception ex)
		{}
		try
		{
			toMinutes  = Integer.parseInt(_formattedHourParts.get("toMinute"));
		}
		catch(Exception ex)
		{
			
		}

		if(((toHour-fromHour)<0 && toHour != 0) || ((toHour-fromHour)==0 && (toMinutes-fromMinute)<0))
		{
			_needsExtension = true;
		}
		
		return _needsExtension;
	}
	
}
